﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace UpdateUploader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        UpdateServices.UpdateServiceClient w;
        private DataTable tblServer;
        private void LoadData()
        {
            gridExistsComponent.AutoGenerateColumns = false;
            tblServer = w.GetUpdateDataSet().Tables[0];
            gridExistsComponent.DataSource = tblServer;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            w = new UpdateUploader.UpdateServices.UpdateServiceClient();
            LoadData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Multiselect = true;
            if (open.ShowDialog() == DialogResult.OK)
            {
                foreach (string fileName in open.FileNames)
                {
                    int index = dataGridView1.Rows.Add();
                    DataGridViewRow row = dataGridView1.Rows[index];
                    row.Cells[0].Value = fileName;
                    row.Cells[1].Value = Path.GetFileName(fileName);
                    row.Cells[2].Value = true;
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            List<DataGridViewRow> lsRow = new List<DataGridViewRow>();
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
            {
                lsRow.Add(row);
            }
            foreach (DataGridViewRow row in lsRow)
            {
                dataGridView1.Rows.Remove(row);
            }

        }
        const int BUFF_SIZE = 51200;
        private void btnUpload_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                Upload(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString());
            }
            LoadData();
            dataGridView1.Rows.Clear();
            MessageBox.Show("Upload completed");
        }

        private void Upload(string fileName, string serverPath)
        {
            FileStream file = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite);
            byte[] buff = new byte[BUFF_SIZE];
            bool isAppend = false;
            do
            {
                int count = file.Read(buff, 0, BUFF_SIZE);
                if (count < BUFF_SIZE)
                {
                    byte[] nBuff = new byte[count];
                    Array.Copy(buff, nBuff, count);
                    w.SaveComponents(nBuff, isAppend, true, serverPath);
                    break;
                }
                else
                {
                    w.SaveComponents(buff, isAppend, false, serverPath);
                }
                isAppend = true;
            }
            while (true);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            foreach (DataRow r in tblServer.Rows)
            {
                if (r.RowState == DataRowState.Modified)
                {
                    w.EnableUpdate(r["Path"].ToString());
                }
            }
            tblServer.AcceptChanges();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you really want to delete selected files?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                List<DataGridViewRow> lsRow = new List<DataGridViewRow>();
                List<string> ls = new List<string>();
                foreach (DataGridViewRow row in gridExistsComponent.SelectedRows)
                {
                    lsRow.Add(row);
                    ls.Add(row.Cells[0].Value.ToString());
                }
                if (ls.Count > 0)
                    w.DeleteFile(ls.ToArray());
                foreach (DataGridViewRow row in lsRow)
                {
                    gridExistsComponent.Rows.Remove(row);
                }
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (gridExistsComponent.SelectedRows.Count > 0)
            {
                List<DataGridViewRow> lsRow = new List<DataGridViewRow>();
                List<string> ls = new List<string>();
                foreach (DataGridViewRow row in gridExistsComponent.SelectedRows)
                {
                    lsRow.Add(row);
                    ls.Add(Path.GetFileName(row.Cells[0].Value.ToString()).ToLower());
                }
                OpenFileDialog open = new OpenFileDialog();
                open.Multiselect = true;
                if (open.ShowDialog() == DialogResult.OK)
                {
                    foreach (string fileName in open.FileNames)
                    {
                        int index = ls.IndexOf(Path.GetFileName(fileName).ToLower());
                        if (index >= 0)
                        {
                            DataGridViewRow dr = lsRow[index];
                            index = dataGridView1.Rows.Add();
                            DataGridViewRow row = dataGridView1.Rows[index];
                            row.Cells[0].Value = fileName;
                            row.Cells[1].Value = dr.Cells[0].Value;
                            row.Cells[2].Value = true;
                        }
                    }
                }
            }
        }
        private void UploadServiceComponent(string fileName)
        {
            string fName = Path.GetFileName(fileName);
            FileStream file = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite);
            byte[] buff = new byte[BUFF_SIZE];
            bool isAppend = false;
            do
            {
                int count = file.Read(buff, 0, BUFF_SIZE);
                if (count < BUFF_SIZE)
                {
                    byte[] nBuff = new byte[count];
                    Array.Copy(buff, nBuff, count);
                    w.SaveServerService(nBuff, isAppend, true, fName);
                    break;
                }
                else
                {
                    w.SaveServerService(buff, isAppend, false, fName);
                }
                isAppend = true;
            }
            while (true);
        }
        private void btnUpdateService_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Multiselect = true;
            if (open.ShowDialog() == DialogResult.OK)
            {
                w.StopServer();
                foreach (string fileName in open.FileNames)
                {
                    UploadServiceComponent(fileName);
                }
                w.StartServer();
            }
        }
    }
}
